package com.dwxt.community.controller;

import com.dwxt.common.base.*;
import com.dwxt.common.util.*;
import com.dwxt.community.aiequipment.util.*;
import com.github.binarywang.utils.qrcode.*;
import org.apache.commons.io.*;
import org.apache.commons.lang3.*;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.*;
import java.nio.charset.*;
import java.util.*;

/**
 * @author
 * @program: intelligentcity
 * @description:
 * @create: 2020/04/26
 */
@RestController
public class TestAiBinRecord {
    @RequestMapping("/testFiddler")
    public String test(HttpServletRequest request) {
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            //根据名称获取请求头的值
            String value = request.getHeader(name);
            System.out.println(name + "---" + value);
        }
        return "success";
    }

    public static void main(String[] args) throws IOException {

        String url = "http://192.168.1.6:2001/cms/fl/ws";
        //url = "http://192.168.1.6:2001/cms/fl/record";
        byte[] data = new byte[]{65, 66, 77, 0, 67, 5, 85, 32, 54, 53, 72, 77, 5, -40, -1, 48, 89, 86, 4, 19, 15, 18, 70, 98, 70, 26, 0, 0, 0, 0, 0, 0, 0, 0, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                4,
                0, 32, 24, 0, -115, -1,
                0, 0, 23, 56, -114, 0,
                0, 0, 24, 40, 88, 6,
                0, 0, 23, 0, 92, 5,
                73, -8};
        smj(url, data);

    }

    public static int[] smj(String url, byte[] res) {
        System.out.println(Arrays.toString(res));
        URL u = null;
        HttpURLConnection con = null;
        InputStream inputStream = null;
        try {
            u = new URL(url);
            con = (HttpURLConnection) u.openConnection();
            con.setRequestMethod("GET");
            con.setDoOutput(true);
            con.setDoInput(true);
            con.setUseCaches(false);
            con.setRequestProperty("user-agent", "");
            con.setRequestProperty("accept", "");
            con.setRequestProperty("Content-Type", "");
            con.setRequestProperty("connection", "");
            OutputStream outStream = con.getOutputStream();
            outStream.write(res);
            outStream.flush();
            outStream.close();
            //读取返回内容
            inputStream = con.getInputStream();
            byte[] bytes = IOUtils.toByteArray(inputStream);
            int[] ints = DataHandler.toIntArray(bytes);
            System.out.println("回复数据byte => " + Arrays.toString(bytes));
            System.out.println("回复数据int => " + Arrays.toString(ints));
            return ints;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (con != null) {
                con.disconnect();
            }
        }
        return null;
    }

    public static void generetorQrCode(int[] ints) throws IOException {
        LinkedList<Integer> integers = new LinkedList<>();
        for (int i = ints.length - 3; i > 0; i--) {
            if (ints[i] == integers.size()) {
                break;
            }
            integers.add(ints[i]);
        }
        int[] ress = new int[integers.size()];
        for (int i = ress.length - 1; i >= 0; i--) {

            ress[i] = integers.get(ress.length - 1 - i);
        }
        System.out.println(DataHandler.toCharArrayStr(ress));
        byte[] qrcode = QrcodeUtils.createQrcode(DataHandler.toCharArrayStr(ress), null);
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("C:\\Users\\MMidoooo\\Desktop\\qrcode.png")));
        bos.write(qrcode);
        bos.flush();
        bos.close();
    }

    /**
     * @description 测试扫码投放  扫码后调用接口
     * @url /cms/aiBin/test
     */
    @RequestMapping("/test")
    public BaseResult test() {
        byte[] userIdBytes = PartyUtil.getCurrentUserId().getBytes(StandardCharsets.UTF_8);
        Calendar instance = Calendar.getInstance();
        byte[] data = new byte[]{65, 66, 42, 0, 67, 33, 86, 20, 51, 57, 70, 77, 5, -45, -1, 51, 27, 5, 0, 0, 0, 0, (byte) Integer.parseInt(String.valueOf(instance.get(Calendar.YEAR)).substring(2)), (byte) instance.get(Calendar.MONTH), (byte) instance.get(Calendar.DAY_OF_MONTH), (byte) instance.get(Calendar.HOUR_OF_DAY), (byte) instance.get(Calendar.MINUTE), (byte) instance.get(Calendar.SECOND)};
        byte[] bytes1 = ArrayUtils.addAll(data, userIdBytes);
        byte[] bytes2 = ArrayUtils.addAll(bytes1, (byte) 0, (byte) 0, (byte) 0);
        bytes2[2] = (byte) bytes2.length;
        bytes2[16] = (byte) (userIdBytes.length + 11 + 1);
        byte[] res = DataHandler.toByteArray(DataHandler.buildCrcArr(DataHandler.toIntArray(bytes2)));
        URL u = null;
        HttpURLConnection con = null;
        InputStream inputStream = null;
        try {
            //u = new URL("http://192.168.1.6:2011/cms/fl/ws");
            u = new URL("http://192.168.1.6:2011/cms/fl/record");
            //u = new URL("http://192.168.1.6:2011/cms/fl/verification");
            //u = new URL("http://125.62.47.85:20002/cms/fl/ws");
            con = (HttpURLConnection) u.openConnection();
            con.setRequestMethod("POST");
            con.setDoOutput(true);
            con.setDoInput(true);
            con.setUseCaches(false);
            con.setRequestProperty("Content-Type", "binary/octet-stream");
            OutputStream outStream = con.getOutputStream();
            outStream.write(res);
            outStream.flush();
            outStream.close();
            //读取返回内容
            inputStream = con.getInputStream();
            byte[] bytes = IOUtils.toByteArray(inputStream);
            int[] ints = DataHandler.toIntArray(bytes);
            return BaseResult.ok(Arrays.toString(ints));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (con != null) {
                con.disconnect();
            }
        }
        return BaseResult.ok();
    }
}
